package com.ruoyi.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.qo.BuyListQO;
import com.ruoyi.business.service.BuyService;
import com.ruoyi.business.vo.BuyVO;
import com.ruoyi.common.common.ResultBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @title: UserController
 * @copyright: copyright (c) 2023
 * @author: jiangguanjun
 * @date: 2023/2/8 10:20
 * @firstReview:
 * @lastReview:
 * @desc: 购买相关
 */
@Api(value = "BuyController", tags = "购买相关")
@RestController
@Slf4j
@RequestMapping("/buy")
public class BuyController {
    @Autowired
    private BuyService buyService;

    @CrossOrigin
    @GetMapping("/list")
    @ApiOperation(value = "采购-购买记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyStartTime", value = "购买开始时间戳", required = false,dataType = "Long"),
            @ApiImplicitParam(name = "buyEndTime", value = "购买结束时间戳" ,required = false, dataType = "Long"),
            @ApiImplicitParam(name = "openId", value = "openid" ,required = false, dataType = "String"),
            @ApiImplicitParam(name = "cId", value = "类目id" ,required = false, dataType = "Long"),
            @ApiImplicitParam(name = "nike", value = "昵称" ,required = false, dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题" ,required = false, dataType = "String")
    })
    public ResultBody<IPage<BuyVO>> list(@RequestParam(name = "buyStartTime",required = false) String buyStartTime,
                                         @RequestParam(name = "buyEndTime",required = false)String buyEndTime,
                                         @RequestParam(name="openId",required = false) String openId,
                                         @RequestParam(name="cId",required = false) Long cId,
                                         @RequestParam(name="nike",required = false) String nike,
                                         @RequestParam(name="title",required = false) String title,
                                         @RequestParam(name="uId",required = false) Long uId,
                                         @RequestParam(name="pId",required = false) Integer pId
                                  ){




        BuyListQO buyListQO = new BuyListQO() ;
        if(StringUtils.isNotBlank(buyStartTime) ){
            buyStartTime += " 00:00:00" ;
            buyListQO.setBuyStartDate(buyStartTime);
        }

        if(StringUtils.isNotBlank(buyEndTime)){
            buyEndTime = buyEndTime + " 23:59:59" ;
            buyListQO.setBuyEndDate(buyEndTime);
        }


        buyListQO.setOpenId(openId);
        buyListQO.setcId(cId);
        buyListQO.setTitle(title);
        buyListQO.setNike(nike);
        buyListQO.setuId(uId);
        buyListQO.setpId(pId);

        log.info("buyListQO:::::::::::::::{}",buyListQO);

        return buyService.queryPage(buyListQO) ;
    }


    @CrossOrigin
    @PostMapping("/listByMy")
    @ApiOperation(value = "获取我的商机列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyStartDate", value = "购买开始时间戳", required = false,dataType = "String"),
            @ApiImplicitParam(name = "buyEndDate", value = "购买结束时间戳" ,required = false, dataType = "String")
    })
    public ResultBody<IPage<BuyVO>> list(@RequestBody BuyListQO buyListQO){


        if(StringUtils.isNotBlank(buyListQO.getBuyStartDate()) ){
            String start = buyListQO.getBuyStartDate() + " 00:00:00" ;
            buyListQO.setBuyStartDate(start);
        }

        if(StringUtils.isNotBlank(buyListQO.getBuyEndDate())){
            buyListQO.setBuyEndDate(buyListQO.getBuyEndDate() + " 23:59:59");
        }
        Long uId = SecurityUtils.getUserId() ;

        buyListQO.setuId(uId);

        log.info("buyListQO:::::::::::::::{}",buyListQO);

        return buyService.queryPage(buyListQO) ;
    }

    @CrossOrigin
    @PostMapping("/deleteById")
    @ApiOperation(value = "采购-购买记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "记录id", required = true,dataType = "Long"),

    })
    public ResultBody deleteById(@RequestBody JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id") ;



        Integer res = buyService.deleteById(id) ;
        if(res>0){
            return ResultBody.success() ;
        }

        return ResultBody.error("删除记录失败") ;
    }

}

